<template>
  <el-container>
    <el-header>
      <div @click='goToHome'>
        <img src='../assets/2.png' />
        <span>校园疫情管理系统</span>
      </div>
      <el-dropdown @command="handleCommand">
          <span style='color: #ffffff;font-size: 17px'>
            {{ admin.adminName }}<i class='el-icon-arrow-down el-icon--right'></i>
          </span>
        <el-dropdown-menu slot='dropdown'>
          <el-dropdown-item command="a">修改密码</el-dropdown-item>
          <el-dropdown-item command="b">退出登陆</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
      <el-form :model="admin">
        <el-form-item label="旧密码" label-width="100px">
          <el-input v-model="admin.oldPassWord" show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" label-width="100px">
          <el-input v-model="admin.newPassWord" show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" label-width="100px">
          <el-input v-model="admin.newPassWord1" show-password autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="changePassword">确 定</el-button>
      </div>
    </el-dialog>
    <el-container>
      <el-aside width='200px'>
        <el-menu
          background-color='#e6edf3'
          text-color='#000000'
          active-text-color='#165daa'
          unique-opened
          router
          :default-active='activePath'
        >
          <el-menu-item v-for='item in menuList' :key='item.id' :index='"/" + item.path'>
            <template #title>
              <i :class='iconsObject[item.id]'></i>
              <span>{{ item.authName }}</span>
            </template>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <!--内容-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [{
        id: 101,
        authName: '用户管理',
        path: 'user'
      }, {
        id: 102,
        authName: '学生管理',
        path: 'student'
      }, {
        id: 103,
        authName: '健康信息管理',
        path: 'bodyTemperature'
      }, {
        id: 104,
        authName: '统计分析',
        path: 'statistical'
      }],
      iconsObject: {
        '101': 'el-icon-user-solid',
        '102': 'el-icon-s-custom',
        '103': 'el-icon-s-marketing',
        '104': 'el-icon-s-data'
      },
      activePath: '',
      admin: {
        adminName: window.sessionStorage.getItem('adminName'),
        oldPassWord: '',
        newPassWord: '',
        newPassWord1: ''
      },
      dialogFormVisible: false
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    goToHome() {
      this.$router.push('/welcome')
    },
    handleCommand(command) {
      if (command === 'a') {
        this.dialogFormVisible = true
      }
      if (command === 'b') {
        this.logout()
      }
    },
    async changePassword() {
      const user = { adminName: this.admin.adminName, password: this.admin.newPassWord1 }
      const { data: res } = await this.$http.put('/api/admin', user)
      if (res.code !== 0) {
        this.dialogFormVisible = false
        this.$message.error('修改失败')
        return
      }
      this.dialogFormVisible = false
      this.$message.success('修改成功')
      setTimeout(() => {
        this.$message.success('请重新登陆')
        this.logout()
      }, 1500)
    }
  }
}
</script>

<style lang='less' scoped>
.el-container {
  height: 100%;
}

.el-aside {
  background-color: #e9f0f6;

  .el-menu {
    border-right: none;
  }
}

.el-header {
  background-color: #326ccc;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ffffff;
  font-size: 25px;

  > div {
    display: flex;
    align-items: center;

    > span {
      margin-left: 20px;
    }
  }
}
</style>
